package org.example.serivce.imp;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class BaseService {
    private static SqlSessionFactory factory;
    static {
        try {
            //加载主配置文件
            Reader reader = Resources.getResourceAsReader("mybatis.cfg.xml");
            //创建会话工厂
            factory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 得到会话对象
     * @return 会话对象
     */
    public SqlSession getSession(){
        return factory.openSession();
    }

}
